package com.itcam.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class Demo01Server {

    public static void main(String[] args) {
        try {
            // 创建服务器端Socket对象，端口号为8899
            ServerSocket serverSocket = new ServerSocket(8899);
            System.out.println("服务器启动。。。");
            // 接受客户端请求，获取到Socket对象
            // accept()方法会等待用户发送请求，然后再继续向下执行
            Socket socket = serverSocket.accept();
            // 根据Socket对象获取字节读取流
            InputStream inputStream = socket.getInputStream();
            // 根据字节流读取创建BufferedReader对象
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            // 读取数据
            System.out.println("客户端发送的信息为：" + bufferedReader.readLine());

            System.out.println("-----------------------------------------------------");

            OutputStream outputStream = socket.getOutputStream();
            String message = "Hello world";
            outputStream.write(message.getBytes());
            socket.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
